package org.example.y24.m11;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

class Activity {
    int begin;
    int end;

    public Activity(int begin, int end) {
        this.begin = begin;
        this.end = end;
    }

    @Override
    public String toString() {
        return "开始时间: " + begin + ", 结束时间: " + end;
    }
}

class ActivityComparator implements Comparator<Activity> {
    @Override
    public int compare(Activity a1, Activity a2) {
        return Integer.compare(a1.end, a2.end);
    }
}

public class ActivitySelection {

    public static List<Activity> activitySelection(List<Activity> activities) {
        List<Activity> selected = new ArrayList<>();
        if (!activities.isEmpty()) {
            Collections.sort(activities, new ActivityComparator());
            selected.add(activities.get(0));
            int lastSelectedEnd = activities.get(0).end;
            for (int i = 1; i < activities.size(); i++) {
                if (activities.get(i).begin >= lastSelectedEnd) {
                    selected.add(activities.get(i));
                    lastSelectedEnd = activities.get(i).end;
                }
            }
        }
        return selected;
    }

    public static void main(String[] args) {
        List<Activity> activities = new ArrayList<>();
        activities.add(new Activity(2, 9));
        activities.add(new Activity(7, 15));
        activities.add(new Activity(3, 10));
        activities.add(new Activity(5, 8));
        activities.add(new Activity(6, 12));
        activities.add(new Activity(9, 14));
        activities.add(new Activity(1, 6));
        activities.add(new Activity(4, 13));
        activities.add(new Activity(11, 16));
        activities.add(new Activity(8, 11));
        activities.add(new Activity(0, 5));
        List<Activity> selectedActivities = activitySelection(activities);
        System.out.println("被选择的活动：");
        for (Activity activity : selectedActivities) {
            System.out.println(activity);
        }
    }
}
